<?php

namespace app\admin\controller\auth;

use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\common\controller\Backend;
use app\common\utils\Constant;
use fast\Random;
use fast\Tree;
use think\Db;
use think\Exception;

/**
 * 管理员管理
 *
 * @icon fa fa-users
 * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
 */
class Admin extends Backend
{

    protected $model = null;
    protected $relationSearch = true;
    protected $childrenGroupIds = [];
    protected $childrenAdminIds = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');

        $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
        $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin() ? true : false);

        $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
        $groupIds = $this->auth->getGroupIds();
        Tree::instance()->init($groupList);
        $result = [];
        if ($this->auth->isSuperAdmin())
        {
            $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
        }
        else
        {
            foreach ($groupIds as $m => $n)
            {
                $result = array_merge($result, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n)));
            }
        }
        $groupName = [];
        foreach ($result as $k => $v)
        {
            $groupName[$v['id']] = $v['name'];
        }

        $this->view->assign('groupdata', $groupName);
        $this->assignconfig("admin", ['id' => $this->auth->id]);
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax())
        {

            $childrenGroupIds = $this->auth->getChildrenAdminIds(true);
            $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
                ->column('id,name');
            $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
                ->field('uid,group_id')
                ->select();
            $prefix = config('database.prefix');
            $adminGroupName = [];
            foreach ($authGroupList as $k => $v)
            {
                if (isset($groupName[$v['group_id']]))
                    $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with('property')
                ->where($where)
                ->where($prefix.'user.id', 'in', $this->childrenAdminIds)
                ->where(array($prefix.'user.user_type'=>Constant::USER_TYPE_PROPERTY))
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with('property')
                ->where($where)
                ->where($prefix.'user.id', 'in', $this->childrenAdminIds)
                ->where(array($prefix.'user.user_type'=>Constant::USER_TYPE_PROPERTY))
//                ->field(['password', 'salt', 'token'], true)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => &$v)
            {
                $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
                $v['groups'] = implode(',', array_keys($groups));
                $v['groups_text'] = implode(',', array_values($groups));
            }
            unset($v);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                Db::startTrans();
                try {
                    $count = model('user')->where(array('username' => $params['username']))->count();
                    if ($count) {
                        $this->error('用户名已使用');
                    }
                    $property = $params['property'];
                    unset($params['property']);
                    $property_obj = model('Property')->create($property);

                    $params['salt'] = Random::alnum();
                    $params['password'] = md5(md5($params['password']) . $params['salt']);
                    $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
                    $params['user_type'] = Constant::USER_TYPE_PROPERTY;
                    $params['property_id'] = $property_obj['id'];

                    $admin = $this->model->create($params);
                    $group = $this->request->post("group/a");

                    //过滤不允许的组别,避免越权
                    $group = array_intersect($this->childrenGroupIds, $group);
                    $dataset = [];
                    foreach ($group as $value) {
                        $dataset[] = ['uid' => $admin->id, 'group_id' => $value];
                    }
                    model('AuthGroupAccess')->saveAll($dataset);
                    Db::commit();
                    $this->success();
                }catch (Exception $e){
                    Db::rollback();
                    $this->error('添加物业信息失败！','',$e->getMessage());
                }
            }
            $this->error();
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                $property = $params['property'];
                unset($params['property']);
                $count = model('user')
                    ->where(array('username'=> $params['username']))
                    ->where('id','neq',$ids)
                    ->count();
                if($count){
                    $this->error('用户名已使用');
                }
                if ($params['password'])
                {
                    $params['salt'] = Random::alnum();
                    $params['password'] = md5(md5($params['password']) . $params['salt']);
                }
                else
                {
                    unset($params['password'], $params['salt']);
                }
                model('Property')->where(array('id'=>$row['property_id']))->update($property);
                $row->save($params);

                // 先移除所有权限
                model('AuthGroupAccess')->where('uid', $row->id)->delete();

                $group = $this->request->post("group/a");

                // 过滤不允许的组别,避免越权
                $group = array_intersect($this->childrenGroupIds, $group);

                $dataset = [];
                foreach ($group as $value)
                {
                    $dataset[] = ['uid' => $row->id, 'group_id' => $value];
                }
                model('AuthGroupAccess')->saveAll($dataset);
                $this->success();
            }
            $this->error();
        }
        $grouplist = $this->auth->getGroups($row['id']);
        $groupids = [];
        foreach ($grouplist as $k => $v)
        {
            $groupids[] = $v['id'];
        }
        $property = model('Property')->where(array('id'=>$row['property_id']))->find();
        $row['property'] = $property;
        $this->view->assign("row", $row);
        $this->view->assign("groupids", $groupids);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            Db::startTrans();
            try {
                // 避免越权删除管理员
                $childrenGroupIds = $this->childrenGroupIds;
                $property_id = $this->model->where(array('id'=>$ids))->value('property_id');
                model('Property')->where(array('id'=>$property_id))->delete();
                $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
                    $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
                })->select();
                if ($adminList) {
                    $deleteIds = [];
                    foreach ($adminList as $k => $v) {
                        $deleteIds[] = $v->id;
                    }
                    $deleteIds = array_diff($deleteIds, [$this->auth->id]);
                    if ($deleteIds) {
                        $this->model->destroy($deleteIds);
                        model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
                        $this->success();
                    }
                }
                Db::commit();
            }catch (Exception $e){
                Db::rollback();
                $this->error('删除失败!','',$e->getMessage());
            }
        }
        $this->error();
    }

    /**
     * 批量更新
     * @internal
     */
    public function multi($ids = "")
    {
        // 管理员禁止批量操作
        $this->error();
    }

}
